home *** CD-ROM | disk | FTP | other *** search
- /*
- * ウインドウでのインプット
- *
- * 1990.7.7
- * Copyright T.Kobayashi
- */
- /*
- * 1991.7.10 H.Ogasawara 編集機能の追加, Emacsモード追加
- * 1991.8.19 H.Ogasawara 入力モードに環境変数を参照するよう改良
- * 1991.9.22 H.Ogasawara Command.win でバスエラーになるバグのデバッグ
- * 1991.12.23 H.Ogasawara デバッグ
- * 1992.3.2 H.Ogasawara 全角判別のバグ修正&2バイト半角対応等,全面書換
- * 1993.5.10 H.Ogasawara force-insert と InputGetMode() 追加
- */
- #include <stdio.h>
- #include <sys_doslib.h>
- #include <iocslib.h>
-
- #include "wlib.h"
- #include "parts.h"
-
- #define Ctrl(a) ((a)&0x1f)
-
- static void DrawSetCursor( DrawBuf*, InputClass* );
- int _kk_EmacsMode= FALSE;
- int _kk_force_insert= FALSE;
-
- void
- InputSet( ip, x, y, str, max, attr, font )
- InputClass *ip;
- int x, y;
- char *str;
- int max;
- int attr, font;
- {
- ClipSet( (ClipClass*)ip, x, y, font/2*max, font );
- ip->str=str;
- ip->max=max;
- ip->attr=attr;
- ip->font=font;
- ip->cur= 0;
- ip->curflag=TRUE;
- {
- unsigned char envbuf[256];
- /* _kk_EmacsMode= FALSE;*/
- if( GETENV( "KOWINKEY", 0, envbuf ) >= 0 ){
- if( *envbuf == 'i' ){
- _kk_force_insert= TRUE;
- _kk_EmacsMode= envbuf[2] == 'm';
- }else
- _kk_EmacsMode= envbuf[1] == 'm';
- }
- }
- }
-
- #if 0 /* move to parts.h */
- void InputSetCursor( ip, cur )
- InputClass *ip ;
- int cur ;
- {
- ip->cur = cur ;
- }
-
- int
- InputGetCursor( ip )
- InputClass *ip;
- {
- return ip->cur;
- }
- #endif
-
- int
- InputSetCursorVisible( buf, ip, sw )
- DrawBuf *buf;
- InputClass *ip;
- int sw;
- {
- if( ip->curflag != sw ){
- ip->curflag= sw;
- if( buf ){
- DrawSetCursor( buf, ip );
- return 1;
- }
- }
- return 0;
- }
-
- int
- InputSetDraw( buf, ip )
- DrawBuf *buf;
- InputClass *ip;
- {
- DrawSetSymbol( buf, ip->x1, ip->y1, ip->str, ip->attr, ip->font );
- if( ip->curflag ){
- DrawSetCursor( buf+1, ip );
- return 2;
- }
- return 1;
- }
-
- static unsigned char emacsmap[]={
- /* @ */ Ctrl('@'),
- /* a */ Ctrl('q'),
- /* b */ Ctrl('s'),
- /* c */ Ctrl('c'),
- /* d */ Ctrl('g'),
- /* e */ Ctrl('p'),
- /* f */ Ctrl('d'),
- /* g */ Ctrl('b'),
- /* h */ Ctrl('h'),
- /* i */ Ctrl('i'),
- /* j */ Ctrl('j'),
- /* k */ Ctrl('k'),
- /* l */ Ctrl('l'),
- /* m */ Ctrl('m'),
- /* n */ Ctrl('f'),
- /* o */ Ctrl('o'),
- /* p */ Ctrl('a'),
- /* q */ Ctrl('v')
- };
-
- int
- InputKey( buf, ip, keycode, shift )
- DrawBuf *buf;
- InputClass *ip;
- int keycode,
- shift;
- {
- DrawBuf *top= buf;
- short cur= ip->cur;
- short px= -1;
- unsigned char *str= (unsigned char*)ip->str;
- static short Kanji,
- Cntrl,
- ChangeMode;
-
- if( cur >= ip->max )
- cur= ip->max-1;
-
- if( ip->curflag )
- DrawSetCursor( buf++, ip ); /* cursor off */
- if( Cntrl ){
- ChangeMode= Cntrl= FALSE;
- goto default_;
- }else if( ChangeMode ){ /*[CTRL]+[_],[e] emacs*/
- ChangeMode= Cntrl= FALSE;
- _kk_EmacsMode= (keycode == 'e');
- keycode= 0;
- }else if( _kk_EmacsMode && keycode<=17 )
- keycode= emacsmap[keycode];
-
- switch( keycode ){
- case Ctrl('h'): /* back space */
- if( cur ){
- cur--;
- if( KanjiTestPosition( str, cur ) )
- cur--;
- }else
- break;
- case Ctrl('k'): /* delete from cursor position */
- case Ctrl('g'): /* delete 1 char */
- case FuncKeyDel:
- deletechar:
- if( str[cur] ){
- short i,
- xx;
- if( keycode == Ctrl('k') ){
- i= KanjiGetPosition( str+cur, -1 );
- str[cur]= '\0';
- }else{
- i= KanjiIsDbcs( str[cur] ) ? 2 : 1;
- strcpy( str+cur, str+cur+i );
- px= cur;
- }
- xx= ip->x1+ KanjiGetPosition( str, -1 )* (ip->font >>1);
- DrawSetLine( buf++, xx, ip->y1,
- xx+(i+1)*(ip->font>>1)-1,ip->y1+ip->font-1,
- (ip->attr & AttrReverse) ? ip->attr & 3 : 0,
- OptionFill );
- }
- break;
- case Ctrl('s'): /* cursor left */
- case FuncKeyLeft:
- if( cur ){
- cur--;
- if( KanjiTestPosition( str, cur ) )
- cur--;
- }
- break;
- case FuncKeyUp:
- case Ctrl('a'):
- if( cur ){
- for( cur-- ; cur && isspace( str[cur] ) ; cur-- );
- for(; cur && !isspace( str[cur] ) ; cur-- );
- if( isspace( str[cur] ) )
- cur++;
- }
- break;
- case Ctrl('d'): /* cursor right */
- case FuncKeyRight:
- if( cur < ip->max-1 && str[cur] ){
- cur++;
- if( KanjiTestPosition( str, cur ) )
- cur++;
- }
- break;
- case FuncKeyClr:
- case Ctrl('b'): /* exchange cursor position */
- if( !cur )
- goto end_of_line;
- case Ctrl('q'): /* top of line */
- cur= 0;
- break;
- case Ctrl('p'): /* end of line */
- end_of_line:
- cur= strlen( str );
- break;
- case FuncKeyHome:
- case Ctrl('u'): /* kill line */
- cur= 0;
- keycode= Ctrl('k');
- goto deletechar;
- case FuncKeyDown:
- case Ctrl('f'):
- for(; str[cur] && !isspace( str[cur] ) ; cur++ );
- for(; str[cur] && isspace( str[cur] ) ; cur++ );
- break;
- case Ctrl('v'):
- Cntrl= TRUE;
- break;
- case Ctrl('o'):
- LEDMOD( 4, !(K_SFTSNS() & 0x100) );
- break;
- case Ctrl('_'):
- ChangeMode= TRUE;
- case 0:
- break;
- default:
- if( ( keycode & 0xff ) >= ' ' ){
- default_:
- /* INS mode */
- if( (_kk_force_insert || (shift & 0x1000)) &&
- strlen(str) < ip->max ){
- memcpy( str+cur+1, str+cur, strlen(str)-cur+1 );
- }else{
- if( !str[cur] )
- str[cur+1]= '\0';
- }
- str[cur]= keycode;
- if( !Kanji ){
- if( KanjiIsDbcs( keycode ) ){
- Kanji= TRUE;
- }else
- px= cur;
- }else{
- px= cur-1;
- Kanji= FALSE;
- }
- if( cur < ip->max-1 )
- cur++;
- }
- } /* end of switch */
- if( px >= 0 ){
- DrawSetSymbol( buf++,
- ip->x1+KanjiGetPosition( str, px )*(ip->font>>1), ip->y1,
- (char*)str+px, ip->attr, ip->font );
- }
- ip->cur= cur;
- if( ip->curflag )
- DrawSetCursor( buf++, ip ); /* cursor on */
- return buf-top;
- }
-
- static void
- DrawSetCursor( buf, ip )
- DrawBuf *buf;
- InputClass *ip;
- {
- short font= ip->font >>1,
- x= ip->x1 + KanjiGetPosition( ip->str, ip->cur )*font,
- y= ip->y1 + font;
- DrawSetLine( buf, x, y, x+font-1, y+font-1, (ip->attr&3)|OptionXor, OptionFill );
- }
-